- A JSP is a servlet
-
Elements
-
scriptlets
- <% ... %>
- Java code can be here
- Code that will land in the "service method"
-
directives
- Give instructions to the container at page translation time
-
page directive
-
import
- <%@ page import="foo.*" %>
- <%@ page import = "foo.*, java.util.*" %>
- multiple packages are separated by commas ","
-
page properties
- Character Encoding
- Content type for the response
- Session object in the page
-
attributes
- 13!
- Just four are covered in the exam :-)
- import
- java import
- isThreadSafe
- true
- Do Not implement
- SingleThreadModel
- I am Thread safe
- false
- Implement
- SingleThreadModel
- Bad idea
- contentType
- defines MIME
- isELIgnored
- Ignore EL when the page is translated
- isErrorPage
- Define a JSP error page
- default value
- false
- true
- Access to the exception object
- errorPage
- Defines URL to send uncaught Throwables
- if is a JSP
- isErrorPage = "true"
-
taglib directive
- Defines tag libraries
-
include directive
-
Text and coded added to the
page at translation time
- Headers
- Footers
- Navigation Bar
- Avoid code duplication
-
expressions
-
<%= .... %>
-
<%= Counter.getCount() %>
- There is no need
of semicolon here
- <% out.print(Counter.getCount()); %>
-
the expression NEVER can be
a method with a void return
- otherwise ERROR
-
Arument of a "print()" methods
- No semicolons!
-
declaration
-
<%! ... %>
- <%! int count = 0; %>
-
Declare members of the generated servlet class
- Variables
- can be
static ones ;)
- Instance Variables
- Methods
- Declarations
-
What really happens
-
The translation
-
The real Scenario in
the Container
- 01. Looks at the directives, for information that might need during the translation
-
02. Creates a HttpServlet subclass
- Vendor specific
-
03. Check if there are packages to import
- Page directive with an import attribute
-
04. If there are declarations
- Write them after the class declaration and before the "service method"
-
05. Builds the service method
- _jspService()
- overriding the Servlet service() method
- The container declare and initialize the "Implicit Objects"
-
06. Combines HTML + scriptlets + expressions
- in the _jspService()
-
Implicit Objects
-
JspWritter
- Is not in the class hierarchy of:
- PrintWritter
- though
- It has the same methods
- adds some buffering capabilities
-
The API of the generated Servlet
-
jspInit()
- method called from the
init() method
-
jspDestroy()
- method called from the
destroy() method
-
_jspService()
- called by the service() method
- Diferent thread
-
Container passes
- Request
- Response
-
Lifecycle of a JSP
-
Translate to java code
- Sintax Errors are caught here!
-
Compile the code generated
- Java language syntax is caught here
- The container loads the newly generated servlet class
-
The Container instantiates the servlet and runs the jspInit() method
- The object now is a servlet
- The container now creates a new thread to handle the client request
and the _jspService() method runs
-
Important note
-
Translation and Compilation only happens once!
- On the first request
-
It is also possible to translate/compilation in advance
- Vendor dependent
- Not guaranteed
-
JSP procompilation protocol
- jsp_precompile
- The container then do
- Translation
- Compilation
-
Initializing the JSP
-
Configuring the INIT parameters in the DD
-
<servlet>
<servlet-name>...</servlet-name>
<jsp-file>/TestInit.jsp</jsp-file>
<init-param>
<param-name />
<param-value ?>
</init-param>
</servlet>
- <jsp-file />
-
Overriding jspInit()
- ServletConfig
- ServletContext
- Available to the Servlet, because
the init() method has already run.
-
Attributes in JSP
-
Servlet
-
Application
- getServletContext().setAttribute("key", Object);
-
Request
- request.setAttribute("key", Object)
-
Session
- request.getSession().setAttribute("key", Object);
-
Page
- NO
-
JSP
-
Application
- application.setAttribute("key", Object)
-
Request
- request.setAttribute("key", Object)
-
Session
- session.setAttribute("key", Object)
-
Page
- pageContext.setAttribute("key", Object)
-
Page Context
- It is possible to get attributes from any scope
-
Page Context
-
getAttribute(String name, int scope )
- scope
- APPLICATION_SCOPE
- PAGE_SCOPE
- REQUEST_SCOPE
- SESSION_SCOPE
- setAttribute(String name, Object object, int scope)
-
getAttribute(String name)
- Page Scope
- getAttributeNamesInScope(int scope)
-
findAttribute(String name)
- Search for the attribute
in all the four scopes
- Page
- Request
- Sesion
- Application
-
same in Servlets
- getServletConfig()
- getServletContext()
- getRequest()
- getSession()
-
Expression Language
EL
-
Expressions looks like this:
- ${something}
-
${applicationScope.mail}
- <%= application.getAttribute("mail") %>
-
Ingnoring EL in the Container
-
First of all
- EL is enabled by default!
-
ingnoring it
-
DD
- <el-ignored> true </el-ignored>
- <jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<el-ignored> true </el-ignored>
</jsp-property-group>
</jsp-config>
-
Page Directive
- <%@ page isELIgnored = "true" %>
-
Invalidate Scripting Elements in a JSP
-
what
- scriptlets
- Java expressions
- declarations
-
how
-
<scripting-invalid> true </scripting-invalid>
- <jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<scripting-invalid>true</scripting-invalid>
</jsp-property-group>
</jsp-config>
-
Actions
-
Standard
- <jsp:include page="myFooter.jsp" />
-
Other
- <c:set var="rate" value="32" />
-
Notes:
-
Template Text is just
- TEXT!!! in middle of HTML or JSP tags
-
Another implicit variable is
- exception